using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AssetFrame.Message;
using System.Reflection;
using SysFrame.BP;
using SysFrame.Message;
using System.Data;

namespace AssetFrame.BP
{
    /// <summary>
    /// asset_a1方法
    /// </summary>
    public class BLLasset_a1
    {
        public Iasset_a1 interface_asset_a1;

        public BLLasset_a1()
        {
            object obj = BPBase.GetAchieve("AssetFrame.BP", "asset_a1");
            interface_asset_a1 = (Iasset_a1)obj;
        }


        /// <summary>
        /// 得到asset_a1实体
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public List<Msasset_a1> Getasset_a1MsByQuery(MsQuery msq)
        {
            return interface_asset_a1.Getasset_a1MsByQuery(msq);
        }

        /// <summary>
        /// 通过ASSET_A1_AUTOID得到实体
        /// </summary>
        /// <param name="ASSET_A1_AUTOID">ID</param>
        /// <returns>实体</returns>
        public Msasset_a1 Getasset_a1MsByKey(string ASSET_A1_AUTOID)
        {
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and ASSET_A1_AUTOID='" + ASSET_A1_AUTOID + "' ";
            List<Msasset_a1> lstData = Getasset_a1MsByQuery(msquery);
            return lstData[0];
        }

        /// <summary>
        ///  保存asset_a1信息
        /// </summary>
        /// <param name="ms">实体信息</param>
        /// <returns>消息</returns>
        public MsReturned Saveasset_a1(Msasset_a1 ms)
        {
            //如果是更新 判断是否需要记录更新日志
            if (ms.ASSET_A1_AUTOID != null && ms.ASSET_A1_AUTOID.ToString() != "" && ms.ASSET_A1_AUTOID.ToString() != "0")
            {
                if (new BLLsystem_4().Getsystem_4ByCode("006001").System_4_50 == "1")
                {
                    Msasset_a1 oldms = Getasset_a1MsByKey(ms.ASSET_A1_AUTOID.ToString());
                    PropertyInfo[] propertys = new Msasset_a1().GetType().GetProperties();
                    //循环属性对比 
                    for (int i = 0; i < propertys.Length; i++)
                    {
                        if (propertys[i].Name.IndexOf("ASSET_A1_") >= 0 && propertys[i].GetValue(oldms, null) != propertys[i].GetValue(ms, null) && propertys[i].GetValue(ms, null) != null)
                        {
                            //时间必定改变
                            if (propertys[i].Name == "ASSET_A1_MAKETIME")
                            {
                                continue;
                            }
                            //说明改变了
                            Mssys_asset_a3 sysa3 = new Mssys_asset_a3();
                            sysa3.SYS_ASSET_A3_10 = ms.ASSET_A1_AUTOID;
                            sysa3.SYS_ASSET_A3_20 = propertys[i].Name;
                            sysa3.SYS_ASSET_A3_30 = propertys[i].GetValue(oldms, null) == null ? "" : propertys[i].GetValue(oldms, null).ToString();
                            sysa3.SYS_ASSET_A3_40 = propertys[i].GetValue(ms, null).ToString();
                            sysa3.SYS_ASSET_A3_50 = ms.typecode1;//动作分类， 来自编辑 批量编辑 接口编辑 还是业务
                            sysa3.SYS_ASSET_A3_60 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            sysa3.SYS_ASSET_A3_70 = ms.typecode2;//动作来源相关ID
                            if (sysa3.SYS_ASSET_A3_30 != sysa3.SYS_ASSET_A3_40)
                            {
                                new BLLsys_asset_a3().Savesys_asset_a3(sysa3);
                            }
                        }
                    }
                }
            }
            return interface_asset_a1.Saveasset_a1(ms);
        }

        /// <summary>
        /// 日期加流水号 临时方法
        /// </summary>
        /// <returns></returns>
        public static string GetMaxSysCode()
        {
            MsQuery msq = new MsQuery();
            string date = DateTime.Now.ToString("yyyyMMdd");
            msq.strquery += " and ASSET_A1_30 like '" + date + "%'";
            DataTable dt = new BLLasset_a1().Getasset_a1DataByQuery(msq);
            return date + (dt.Rows.Count + 1).ToString().PadLeft(4, '0');
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msquery">条件</param>
        /// <returns>数据类型</returns>
        public MsReturned Getasset_a1ByQuery(MsQuery msquery)
        {
            return interface_asset_a1.Getasset_a1ByQuery(msquery);
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msquery">条件</param>
        /// <returns>数据类型</returns>
        public DataTable Getasset_a1DataByQuery(MsQuery msquery)
        {
            return interface_asset_a1.Getasset_a1DataByQuery(msquery);
        }

        public DataTable Getasset_a1GroupDataByQuery(MsQuery msquery, List<string> group)
        {
            return interface_asset_a1.Getasset_a1GroupDataByQuery(msquery, group);
        }

        /// <summary>
        /// 删除asset_a1
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deleteasset_a1ByQuery(MsQuery msq)
        {
            return interface_asset_a1.Deleteasset_a1ByQuery(msq);
        }
        /// <summary>
        /// 删除asset_a1
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deleteasset_a1ByKey(string ASSET_A1_AUTOID)
        {
            MsQuery msquery = new MsQuery();

            if (ASSET_A1_AUTOID.IndexOf(",") > 0)
            {
                msquery.strquery += " and ASSET_A1_AUTOID in (" + ASSET_A1_AUTOID + ") ";
            }
            else
            {
                msquery.strquery += " and ASSET_A1_AUTOID='" + ASSET_A1_AUTOID + "' ";
            }
            return interface_asset_a1.Deleteasset_a1ByQuery(msquery);
        }
    }
}
